package com.zjmzxfzhl.modules.purchase.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseFileQueryDto;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseFileUpdateDto;
import com.zjmzxfzhl.modules.purchase.controller.vo.PurchaseFileVo;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseFile;
import com.zjmzxfzhl.modules.purchase.service.PurchaseFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/file")
public class PurchaseFileController extends BaseController {
    @Autowired
    private PurchaseFileService purchaseFileService;

    /**
     * 自定义查询列表
     *
     * @param purchaseFile
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:file:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseFileQueryDto purchaseFile, @RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        if (null == current || null == size) {
            current = 1;
            size = 1000;
        }
        IPage<PurchaseFileVo> pageList = purchaseFileService.list(new Page<PurchaseFileVo>(current, size), purchaseFile);
        return Result.ok(pageList);
    }


    @RequiresPermissions("purchase:file:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseFile purchaseFile = purchaseFileService.getById(id);
        return Result.ok(purchaseFile);
    }

    /**
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:file:save")
    @PostMapping(value = "/save")
    public Result save(@RequestParam("file") MultipartFile multipartFile,
                       PurchaseFile purchaseFile) {
        purchaseFileService.uploadFile(multipartFile, purchaseFile);
        return Result.ok();
    }

    /**
     * @param purchaseFileUpdateDto
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:file:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseFileUpdateDto purchaseFileUpdateDto) {
        PurchaseFile purchaseFile = BeanUtil.copyProperties(purchaseFileUpdateDto, PurchaseFile.class);
        purchaseFileService.updateById(purchaseFile);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:file:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        purchaseFileService.deleteFiles(Convert.toList(Long.class, idsArr));
        return Result.ok();
    }

    @RequestMapping(value = "/download/{fileId}", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response,
                             @PathVariable("fileId") Long fileId) {
        purchaseFileService.downloadFile(response, fileId);
    }

    @RequestMapping(value = "/preview/{fileId}", method = RequestMethod.GET)
    public void previewFile(HttpServletResponse response,
                            @PathVariable("fileId") Long fileId) {
        purchaseFileService.readFile(response, fileId);
    }
}
